package com.ybxx.base.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ybxx.base.dao.RoomInfoDao;
import com.ybxx.base.model.Community;
import com.ybxx.base.model.Room;
import com.ybxx.base.model.RoomInfo;
import com.ybxx.base.service.RoomInfoService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.dao.GenericDao;
import com.ybxx.core.service.impl.BaseServiceImpl;
import com.ybxx.core.web.paging.PagingBean;

public class RoomInfoServiceImpl extends BaseServiceImpl<RoomInfo> implements
		RoomInfoService {
	private RoomInfoDao roomInfoDao;
	public RoomInfoServiceImpl(RoomInfoDao dao) {
		super(dao);
		this.roomInfoDao=dao;
		// TODO Auto-generated constructor stub
	}
	/*检查房间信息*/
	public int checkConfig(String unitid){
		String hql="from RoomInfo ri right join ri.room r " +
				"left join fetch r.unit u left join fetch u.ban b left join fetch b.community c where ri is null ";
		if(unitid.indexOf("R")==0){
			hql+=" and r.rid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("U")==0){
			hql+=" and u.uid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("B")==0){
			hql+=" and b.bid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("C")==0){
			hql+=" and c.cid='"+unitid.substring(1)+"'";
		}
		return roomInfoDao.findCountByHql(hql, null);
	}
	public Object[] load(String rid){
		String hql="from RoomInfo ri right join ri.room r " +
				"left join fetch r.unit u left join fetch u.ban b left join fetch b.community c " +
				"left join fetch c.glrule gl left join fetch c.roomrule ff "+
				"left join fetch ri.houseType h where r.rid='"+rid+"'";
		Object obj=this.roomInfoDao.findUnique(hql, null);
		return (Object[])obj;
	}
	public List queryPage(QueryFilter filter,String cid,String bid,String uid,String rid,String loginid){
		List rooms=null;
		String hql="select r,ri from RoomInfo ri right join ri.room as r left join fetch r.unit u left join fetch u.ban b left join fetch b.community c left join fetch ri.houseType,DataRights m where r.rid=m.dvalue and m.dtype='R' and m.user.loginid='"+loginid+"'";
		if(StringUtils.isNotEmpty(cid)){
			hql+=" and r.unit.ban.community.cid like '%"+cid+"%'";
		}
		if(StringUtils.isNotEmpty(bid)){
			hql+=" and r.unit.ban.bid like '%"+bid+"%'";
		}
		if(StringUtils.isNotEmpty(uid)){
			hql+=" and r.unit.uid like '%"+uid+"%'";
		}
		if(StringUtils.isNotEmpty(rid)){
			hql+=" and r.rid like '%"+rid+"%'";
		}
		hql+=" order by c.cid,b.bname,u.uname,r.rcode";
		
		rooms=this.roomInfoDao.findByHql(hql, null, filter.getPagingBean());
		return rooms;
	}


}
